<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生注册</title>
    <script src="js/base.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        h1 {
            font-size: 2em;
            color: #333;
            text-align: center;
            margin-bottom: 20px;
        }
        input[type="text"], input[type="password"] {
            width: 30%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
        }
        input[type="radio"] {
            margin-right: 10px;
        }
        button {
            padding: 10px 20px;
            margin: 10px 0;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background-color: #0056b3;
        }
        #usernameAlert, #passwordAlert {
            font-size: 12px;
            margin-left: 5px;
        }
    </style>
</head>
<body>
        <h1>学生注册</h1>
        用户名：<input type="text" id="username" onchange="onUsernameChanged()"/><span style="color:red" id="usernameAlert"></span><br/>
        密码：<input type="password" id="password" onchange="onPasswordChanged();"/><br/>
        密码重复：<input type="password" id="rePassword" onchange="onPasswordChanged();"/><span style="color:red" id="passwordAlert"></span><br/>
        性别：<div class="gender-group">
        <input type="radio" name="gender" id="nan" value="男" checked="checked"/>男<input type="radio" name="gender" id="nv" value="女"/>女<br/>
        </div>
            <button onclick="onSignUpClick()">注册</button>

<script>
    let usernameFlag=false;
    let passwordFlag=false;

    function onUsernameChanged(){
        let username=document.getElementById("username").value;
    $.rest("http://localhost:8080/user/findUsername?username="+username,"get",null,function (r){
        if (r.code==200){
            document.getElementById("usernameAlert").style.color="green";
            document.getElementById("usernameAlert").innerHTML="用户名可用";
            usernameFlag=true;
        }else{
            document.getElementById("usernameAlert").style.color="red";
            document.getElementById("usernameAlert").innerHTML="用户名不可用";
            usernameFlag=false;
        }
    });
    }

    function onPasswordChanged(){
        let password=document.getElementById("password").value;
        let rePassword=document.getElementById("rePassword").value;
        if (password!=rePassword){
            document.getElementById("passwordAlert").style.color="red";
            document.getElementById("passwordAlert").innerHTML="两次密码输入不一致";
            passwordFlag=false;
        }else{
            document.getElementById("passwordAlert").style.color="green";
            document.getElementById("passwordAlert").innerHTML="两次密码输入一致";
            passwordFlag=true;
        }
    }
    function onSignUpClick(){
        if(usernameFlag&&passwordFlag){
            let username=document.getElementById("username").value;
            let password=document.getElementById("password").value;
            let nan=document.getElementById("nan");
            let gender=null;
            if(nan.checked){
                gender="男";
            }else{
                gender="女";
            }

            let json={};
            json.username=username;
            json.password=password;
            json.gender=gender;
          $.rest("http://localhost:8080/user/signUp","Post",json,function(r){
              if(r.code==200){
                  alert("注册成功");
              }else{
                  alert("注册失败");
              }
            });
        }else{
            alert("请检查输入");
        }
    }
</script>

</body>
</html>